package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.*;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.*;
import com.sky.utils.JwtUtil;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "C端用户相关接口")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 微信登录
     *
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/user/login")
    @ApiOperation("微信登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("微信用户登录：{}", userLoginDTO.getCode());

        //微信登录
        User user = userService.wxLogin(userLoginDTO);//后绪步骤实现

        //为微信用户生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取店铺状态
     *
     * @return
     */
    @GetMapping("/shop/status")
    public Result getStatus() {
        String status = (String) redisTemplate.opsForValue().get(StatusConstant.SHOP_STATUS);
        return Result.success(status);
    }

    @Autowired
    private CategoryService categoryService;

    /**
     * 查询分类
     *
     * @param type
     * @return
     */
    @GetMapping("/category/list")
    public Result categortList(Integer type) {
        if (type == null) {
            List<Category> list = categoryService.lambdaQuery().list();
            return Result.success(list);
        }
        List<Category> list = categoryService.lambdaQuery()
                .eq(Category::getType, type).list();

        return Result.success(list);
    }

    @Autowired
    private IDishService dishService;

    @Autowired
    private IDishFlavorService dishFlavorService;

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/dish/list")
    public Result dishtList(@RequestParam("categoryId") Integer categoryId) {
        List<Dish> dishList = dishService.lambdaQuery()
                .eq(Dish::getCategoryId, categoryId).list();
        ArrayList<DishVO> dishVOSList = new ArrayList<>();
        for (Dish dish : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish, dishVO);
            List<DishFlavor> dishFlavors = dishFlavorService.lambdaQuery()
                    .eq(DishFlavor::getDishId, dishVO.getId()).list();
            dishVO.setFlavors(dishFlavors);
            dishVOSList.add(dishVO);
        }
        return Result.success(dishVOSList);
    }

    @Autowired
    private ISetmealService setmealService;

    /**
     * 根据分类id查询套餐
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/setmeal/list")
    public Result setmealList(@RequestParam("categoryId") Integer categoryId) {
        List<Setmeal> setmealList = setmealService.lambdaQuery()
                .eq(Setmeal::getCategoryId, categoryId).list();

        return Result.success(setmealList);
    }

    @Autowired
    private SetmealDishesService setmealDishesService;

    @GetMapping("/setmeal/dish/{id}")
    public Result setmealDishById(@PathVariable("id") Long id) {
        SetmealDish setmealDish = setmealDishesService.lambdaQuery()
                .eq(SetmealDish::getSetmealId, id).one();
        Integer copies = setmealDish.getCopies();
        String name = setmealDish.getName();
        Setmeal setmeal = setmealService.lambdaQuery()
                .eq(Setmeal::getId, id).one();
        String image = setmeal.getImage();
        String description = setmeal.getDescription();
        DishItemVO dishItemVO = DishItemVO.builder().name(name).image(image).description(description).copies(copies).build();
        return Result.success(dishItemVO);
    }
}